<template>
  <el-dialog
    :title="title"
    :visible.sync="open"
    width="600px"
    append-to-body
    :close-on-click-modal="false"
    @close="closeModal"
  >
    <el-form ref="form" :model="form" :show-message="false" label-width="150px">
      <el-form-item label="药品类别名称" prop="categoryName" required>
        <el-input
          v-model="form.categoryName"
          placeholder="请输入药品类别名称"
          :maxlength="12"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="所属处方类型" prop="prescriptionType" required>
        <el-select
          v-model="form.prescriptionType"
          placeholder="请选择处方类型"
          style="width: 100%"
        >
          <el-option
            v-for="dict in dict.type.tcm_prescript_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否发送配药机" prop="machinePush" required>
        <el-radio-group v-model="form.machinePush">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="0">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <!-- <el-form-item label="用量是否允许小数点" prop="isDecimal" required>
        <el-radio-group v-model="form.isDecimal">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="0">否</el-radio>
        </el-radio-group>
      </el-form-item> -->
      <el-form-item label="药品类别简介" prop="introduce">
        <el-input
          type="textarea"
          :rows="3"
          placeholder="请输入药品类别简介"
          v-model="form.introduce"
        >
        </el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="open = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addDrugCategory, updateDrugCategory } from "@/api/saas/tcm.js";
import { deepClone } from '@/utils';

export default {
  dicts: [
    "tcm_prescript_type"
  ],
  data() {
    return {
      title: "新建药品类别",
      open: false,
      form: {
        id: undefined,
        categoryName: "",
        prescriptionType: undefined,
        machinePush: 1,
        isDecimal: 0,
        introduce: ""
      }
    };
  },
  methods: {
    openModal(row) {
      if(row && row.id){
        this.title = "编辑药品类别";
        this.form = deepClone(row);
      }
      this.open = true;
    },
    closeModal() {
      this.$refs["form"].resetFields();
      Object.assign(this.$data, this.$options.data()); // 存在自定义校验规则不能重置data
    },
    submitForm() {
      const that = this;
      that.$refs["form"].validate(async (valid) => {
        if (valid) {
          if (that.form.id) {
            await updateDrugCategory(that.form)
          } else {
            await addDrugCategory(that.form);
          }
          that.$message.success(that.form.id ? '修改成功' : '新增成功');
          that.$emit('refresh');
          that.closeModal()
        }
      });
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
